﻿using System;
using System.Linq;

namespace RaceList.Admin.Relay
{
    public partial class CycleEdit : System.Web.UI.Page
    {
        ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
        protected int ID
        {
            get
            {
                if (Request.QueryString["ID"] != null)
                {
                    return Convert.ToInt32(Request.QueryString["ID"].ToString());
                }
                else
                {
                    return 0;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Data_Bind();
            }
        }
        protected void Data_Bind()
        {
            var cycle = db.Activity_Cycle.SingleOrDefault(s => s.Id == ID);
            if (cycle == null)
            {
                StringHelper.Alert(this, "此信息不存在或已被删除！", "CycleList.aspx");
                return;
            }
            #region 赋值
            txtStartDate.Text = Convert.ToDateTime(cycle.startDate).ToString("yyyy-MM-dd HH:mm:ss");
            txtEndDate.Text = Convert.ToDateTime(cycle.endDate).ToString("yyyy-MM-dd HH:mm:ss");
            txtTitle.Text = cycle.title;
            txtUrl.Text = cycle.url;
            ddl1.SelectedIndex = CommonHelp.GetSelectIndex(cycle.status);
            #endregion
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Activity_Cycle cycle = db.Activity_Cycle.SingleOrDefault(ar => ar.Id == ID);
            cycle.status = CommonHelp.GetStatus(ddl1.SelectedIndex);
            cycle.startDate = Convert.ToDateTime((string) txtStartDate.Text.Trim());
            cycle.endDate = Convert.ToDateTime((string) txtEndDate.Text.Trim());
            cycle.title = txtTitle.Text.Trim();

            try
            {
                db.SubmitChanges();
                StringHelper.Alert(this, "活动修改成功！", "CycleList.aspx");
            }
            catch (Exception ex)
            {
                this.Message.Text = "修改失败! 失败原因：" + ex.Message + "。详细错误信息请查看错误日志。";
            }
        }
    }
}